package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.wanmao.test.bean.GZJLBean;
import com.wanmao.test.dbhelper.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class GZJLService {
    private DBOpenHelper dBOpenHelper;

    public GZJLService(Context context) {
        this.dBOpenHelper = new DBOpenHelper(context);
    }
    public void save(GZJLBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("GZJL_TABLE", null, settingValue(bean));
    }

    private ContentValues settingValue(GZJLBean bean) {
        ContentValues values = new ContentValues();
        values.put("OBJ_ID",bean.getOBJ_ID());
        values.put("RWID",bean.getRWID());
        values.put("BDZMC",bean.getBDZMC());
        values.put("GZXLMC",bean.getGZXLMC());
        values.put("DYDJ",bean.getDYDJ());
        values.put("GZFSSJ",bean.getGZFSSJ());
        values.put("GZLX",bean.getGZLX());
        values.put("SFTZ",bean.getSFTZ());
        values.put("SFTY",bean.getSFTY());
        values.put("GZJT",bean.getGZJT());
        values.put("GZQK",bean.getGZQK());
        return values;
    }
    public List<GZJLBean> findByRWID(String RWID){
        String sql = "select * from GZJL_TABLE where RWID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{RWID});
        return toListBean(cursor);

    }
    public GZJLBean findByOBJID(String OBJID){
        String sql = "select * from GZJL_TABLE where OBJ_ID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{OBJID});
        while (cursor.moveToNext()){
            GZJLBean bean=new GZJLBean();
            bean.setOBJ_ID(cursor.getString(cursor.getColumnIndex("OBJ_ID")));
            bean.setBDZMC(cursor.getString(cursor.getColumnIndex("BDZMC")));
            bean.setGZXLMC(cursor.getString(cursor.getColumnIndex("GZXLMC")));
            bean.setGZLX(cursor.getString(cursor.getColumnIndex("GZLX")));
            bean.setDYDJ(cursor.getString(cursor.getColumnIndex("DYDJ")));
            bean.setGZFSSJ(cursor.getString(cursor.getColumnIndex("GZFSSJ")));
            bean.setSFTY(cursor.getString(cursor.getColumnIndex("SFTY")));
            bean.setSFTZ(cursor.getString(cursor.getColumnIndex("SFTZ")));
            bean.setGZJT(cursor.getString(cursor.getColumnIndex("GZJT")));
            bean.setGZQK(cursor.getString(cursor.getColumnIndex("GZQK")));
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
            return bean;
        }
        return null;

    }

    private List<GZJLBean> toListBean(Cursor cursor){
        List<GZJLBean> list=new ArrayList<>();
        while (cursor.moveToNext()){
            GZJLBean bean=new GZJLBean();
            bean.setOBJ_ID(cursor.getString(cursor.getColumnIndex("OBJ_ID")));
            bean.setBDZMC(cursor.getString(cursor.getColumnIndex("BDZMC")));
            bean.setGZXLMC(cursor.getString(cursor.getColumnIndex("GZXLMC")));
            bean.setGZLX(cursor.getString(cursor.getColumnIndex("GZLX")));
            bean.setDYDJ(cursor.getString(cursor.getColumnIndex("DYDJ")));
            bean.setGZFSSJ(cursor.getString(cursor.getColumnIndex("GZFSSJ")));
            bean.setSFTY(cursor.getString(cursor.getColumnIndex("SFTY")));
            bean.setSFTZ(cursor.getString(cursor.getColumnIndex("SFTZ")));
            bean.setGZJT(cursor.getString(cursor.getColumnIndex("GZJT")));
            bean.setGZQK(cursor.getString(cursor.getColumnIndex("GZQK")));
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
            list.add(bean);
        }
        return list;
    }
}
